﻿using System.Configuration;
using System.Net;
using System.Net.Mail;
using System.Text;
using System.Web.Security;
using com.subb.WebFramework;

namespace WebsiteUI.Extensions
{
    public class EmailUtils
    {
        private string MemberId { get; set; }
        private string SendTo { get; set; }
        private string FromIP { get; set; }
        private string ValidCode { get; set; }

        public void SendVerifyMail(string memId, string sendTo, string fromIp)
        {
            string smtpIp = ConfigurationManager.AppSettings["SmtpAddress"];
            string emailAccount = ConfigurationManager.AppSettings["EmailAccount"];
            string emailPsw = ConfigurationManager.AppSettings["EmailPsw"];
            string subject = ConfigurationManager.AppSettings["EmailValidSubject"];
            string vcode = RandNumSupplier.GetRandomCode(6);
            string cipherText = FormsAuthentication.HashPasswordForStoringInConfigFile(memId + vcode, "md5");
            StringBuilder body = new StringBuilder();
            body.Append("<p>您好，欢迎成为广东省城市垃圾处理行业协会网站的会员！</p>");
            body.Append("<p>您的用户名是：" + memId + "。</p>");
            body.Append("<p>请点击下面链接完成邮箱验证：</p>");
            body.Append("<p><a href='http://www.gduwdia.com/home/regconfirm/" + cipherText + "' target='_blank'>http://www.gduwdia.com/home/regconfirm/" + cipherText + "</a></p>");
            body.Append("<p>如果该链接无法点击，请直接拷贝以上网址到浏览器地址栏中访问。</p>");
            body.Append("<p style='font-weight:bold;color:red;'>本邮件作为找回密码和用户安全验的辅助证据之一，请妥善保管！</p>");
            body.Append("<p>此信是由城市垃圾处理网客服中心发出，不接受回信，请勿直接回复。</p>");
            body.Append("<p style='text-indent:2em'>致礼！</p>");
            SmtpClient client = new SmtpClient(smtpIp);
            client.UseDefaultCredentials = true;
            client.DeliveryMethod = SmtpDeliveryMethod.Network;
            client.Credentials = new NetworkCredential(emailAccount, emailPsw);
            MailMessage mmsg = new MailMessage();
            mmsg.From = new MailAddress(emailAccount);
            mmsg.To.Add(sendTo);
            mmsg.Subject = subject;
            mmsg.Body = body.ToString();
            mmsg.SubjectEncoding = System.Text.Encoding.UTF8;
            mmsg.BodyEncoding = System.Text.Encoding.UTF8;
            mmsg.Priority = System.Net.Mail.MailPriority.Normal;
            mmsg.IsBodyHtml = true;
            //设置属性
            MemberId = memId;
            SendTo = sendTo;
            FromIP = fromIp;
            ValidCode = cipherText;
            client.SendCompleted += new SendCompletedEventHandler(client_SendCompleted);
            client.SendAsync(mmsg, "sendcomplete");
        }

        private void client_SendCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
        {
            if (e.UserState.ToString() == "sendcomplete")
            {
                FW_MemberDAL mdal = new FW_MemberDAL();
                mdal.CreateEmailVcode(MemberId, ValidCode, SendTo, FromIP);
            }
        }

        public bool CheckEmailFormat(string email)
        {
            RegexStringValidator ss = new RegexStringValidator(@"^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$");
            try
            {
                ss.Validate(email);
                return true;
            }
            catch
            {
                return false;
            }
        }
    }
}